package com.zzyk.main.service.impl;

import com.zzyk.main.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements IRedisService {
    @Autowired
    private StringRedisTemplate template;

    @Override
    public String set(String key, String value) {
        ValueOperations<String, String> map = template.opsForValue();
        map.set(key, value);
        return value;
    }

    @Override
    public String set(String key, String value, long timeout) {
        ValueOperations<String, String> map = template.opsForValue();
        map.set(key, value, timeout, TimeUnit.MILLISECONDS);
        return value;
    }

    @Override
    public String get(String key) {
        ValueOperations<String, String> map = template.opsForValue();
        return map.get(key);
    }

    @Override
    public boolean del(String key) {
        return template.delete(key);
    }

    @Override
    public void setExpire(String key, long timeout) {
        template.expire(key, timeout, TimeUnit.MILLISECONDS);
    }

    @Override
    public long getExpire(String key) {
        return template.getExpire(key);
    }

    @Override
    public long increment(String key) {
        ValueOperations<String, String> map = template.opsForValue();
        return map.increment(key, 1);
    }

    @Override
    public long decrement(String key) {
        ValueOperations<String, String> map = template.opsForValue();
        return map.decrement(key, 1);
    }

    @Override
    public String push(String key, String value) {
        ListOperations<String, String> list = template.opsForList();
        list.rightPush(key, value);
        return value;
    }

    @Override
    public String pop(String key) {
        ListOperations<String, String> list = template.opsForList();
        return list.rightPop(key);
    }

    @Override
    public String unshift(String key, String value) {
        ListOperations<String, String> list = template.opsForList();
        list.leftPush(key, value);
        return value;
    }

    @Override
    public String shift(String key) {
        ListOperations<String, String> list = template.opsForList();
        return list.leftPop(key);
    }

    @Override
    public Object set(String key, Object item, Object obj) {
        HashOperations<String, Object, Object> hash = template.opsForHash();
        hash.put(key, item, obj);
        return obj;
    }

    @Override
    public Object get(String key, Object item) {
        HashOperations<String, Object, Object> hash = template.opsForHash();
        return hash.get(key, item);
    }

    @Override
    public Object del(String key, Object item) {
        HashOperations<String, Object, Object> hash = template.opsForHash();
        Object obj = hash.get(key, item);
        hash.delete(key, item);
        return obj;
    }
}
